<?php
/**
 * 功能：解析Url地址，把Page参数去掉组合成一个地址
 * 返回：翻页地址
 */
function ParseUrl()
{
	$url = $_SERVER['REQUEST_URI'];					//提取当前地址
	if(strpos($url, '?'))										//判断是否有携带参数
	{
		$file = substr($url, 0,strpos($url, '?'));						//取得文件地址
		$para = substr($url,strpos($url,'?')+1,strlen($url));			//取得参数字符串
		$array = explode('&',$para);							//分解参数
		for($i=0;$i<sizeof($array);$i++)							//循环组合参数
		{
			$tmp = array();
			$tmp = explode("=",$array[$i]);						//分解参数名和值
			if($tmp[0] != 'Page')								//判断参数是否为Page
			{											//不是则组合参数
				if($i != 0)									//判断是否为第一个参数,不是则加’&’
					$link .= "&";
				$link .= $array[$i];
			}
		}
		$link = $file . "?" . $link . "&";							//组合成翻页连接地址
	}else{
		$link = $url . "?";
	}
	return $link;
}
/**
 * 功能：输出翻页字符串
 * 参数：$pagecount 总页数,$curr_page 当前页码,$pagesize 每页文章数
 * 返回：翻页字符串
 */
function Page($pagecount,$curr_page,$pagesize,$postcount)
{
	$url = ParseUrl();
	$prev = $curr_page - 1;
	$next = $curr_page + 1;
	$start = "<a href=\"" . $url . "Page=1\" title=\"首页\">&lt;&lt;</a>";
	$prev_link = ($prev >= 1) ? "<a href=\"" . $url . "Page=$prev\" title=\"上一页\">&lt;</a>" : "<a href=\"javascript:void(0)\" title=\"上一页\">&lt;</a>";
	$next_link = ($next <= $pagecount) ? "<a href='" . $url . "Page=$next'>&gt;</a>" : "<a href=\"javascript:void(0)\" title=\"下一页\">&gt;</a>";
	$curr_link = "";
	for($i=1; $i<=$pagecount; $i++){
		if($curr_page == $i)
			$curClass = "current";
		else
			$curClass = "";
		$curr_link .= "<a class=\"".$curClass."\" href=\"" . $url . "Page=$i\">$i</a>";
	}
	$end = "<a href='" . $url ."Page=$pagecount' title=\"尾页\">&gt;&gt;</a>";
	$str = "<span>共 <em>" . $pagecount . "</em> 页， 每页显示 <em>" . $pagesize ."</em> 篇， 共 <em>" . $postcount . "</em> 篇</span>";
	$str .= "<span class=\"sec2\">" . $start . $prev_link . $curr_link . $next_link . $end ."</span>";
	return $str;
}
/*
 * 功能：上传图片文件，并返回上传文件的数组信息
 * 参数：$_FILES对象，上传路径$file_path, 上传文件大小限制$file_max_size
 * 返回：一个数组对象
 */
function upload($file_info, $file_path, $file_max_size){
	$upload_data = array();

	//取得数组中的值 extract($file_info);
	if($file_info['error']==UPLOAD_ERR_OK){

		$find_type = strtolower(pathinfo($file_info['name'], PATHINFO_EXTENSION));
		$upload_type = ".jpg,.jpeg,.gif,.png,.bmp";
		$allow_type = strpos($upload_type, $find_type);
		$file_size = $file_info['size']/1024;

		//判断上传的文件类型
		/*
		if($allow_type === FALSE)
		{
			echo "文件类型不对";
			exit();
		}
		*/

		//判断上传文件大小是否为0
		if($file_info['size'] == 0)
		{
			echo "文件大小不能为0";
			exit();
		}

		//判断上传文件大小是否超过限制
		if($file_size > $file_max_size)
		{
			echo "上传文件大小超出最大值, 请重新上传";
			exit();
		}

		//var_dump($file_info);

		$upload_data['F_UPLOAD_NAME'] = $file_info['name'];
		$upload_data['F_UPLOAD_TYPE'] = $file_info['type'];
		$upload_data['F_UPLOAD_SIZE'] = $file_size;

		$file_moved = $file_path.$file_info['name'];

		$ret = @move_uploaded_file($file_info['tmp_name'], $file_moved);

		//判断是否上传成功
		if($ret === FALSE)
			echo "Unable to move user photo!<br />\n";
		else {
			//echo "Moved user avatar to photos directory<br />\n";
		}

	} else {
		switch($file_info['error'])
		{
			case UPLOAD_ERR_INI_SIZE:
			case UPLOAD_ERR_FORM_SIZE:
				echo "FileSize Out";
			case UPLOAD_ERR_PARTIAL:
			echo "IncompleteUpload";
				break;
			default:
				echo "say what?";
				break;

		}
	}

	return $upload_data;

}
/*
 * 功能：读取文件夹所有文件，并生产一个数字
 * 参数：文件夹路径$file_path
 * 返回: 一个数字
 */
function file_nameed($file_path){

	$file_new_Num = array();

	$file_all = glob("$file_path *.*");
	foreach($file_all as $filename){
		$s_org = basename($filename);
		$s_start = 0;
		$s_end = strpos($s_org, ".");
		$s_name = substr($s_org, $s_start, $s_end);
		array_push($file_new_Num, $s_name);
	}

    return $file_new_Num;

}

/**
* 功能：截取utf8字符串
* 参数：$strutf8
* 返回：字符串$sUtf8
*/
function utf8Substr($str, $from, $len)
{
    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                       '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
                       '$1',$str);
}
?>